﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class GroupValueManage
	{
		public GroupValueManage(Autodesk.Revit.DB.Document document, Reference reference, List<FamilySymbol> list, double distance)
		{
			this.nStartType = 1;
			this.doc = document;
			this.refer = reference;
			this.listFamilySymbol = list;
			this.setDistance = distance;
			this.Initial();
		}

		public GroupValueManage(Autodesk.Revit.DB.Document document, Reference reference, List<FamilySymbol> list, XYZ pGlobal)
		{
			this.nStartType = 2;
			this.doc = document;
			this.refer = reference;
			this.listFamilySymbol = list;
			this.Initial();
			this.ChickPoint();
			this.point = pGlobal;
		}

		public GroupValueManage(Autodesk.Revit.DB.Document document, Pipe RefPipe, XYZ pointStart, double distance, List<FamilySymbol> list)
		{
			this.nStartType = 1;
			this.doc = document;
			this.listFamilySymbol = list;
			this.setDistance = distance;
			this.pipe = RefPipe;
			this.diameter = this.pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
			this.pipePoint = YJKRevitTools.OrderPipePoint(this.pipe, pointStart);
			this.point = this.GetXYZByDistance(this.pipePoint[1], this.pipePoint[0], this.setDistance / 304.8);
		}

		public GroupValueManage(Autodesk.Revit.DB.Document document, Pipe RefPipe, XYZ pointStart, double distance, List<FamilySymbol> list, bool IsShowMessage)
		{
			this.nStartType = 1;
			this.doc = document;
			this.listFamilySymbol = list;
			this.setDistance = distance;
			this.pipe = RefPipe;
			this.diameter = this.pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
			this.pipePoint = YJKRevitTools.OrderPipePoint(this.pipe, pointStart);
			this.point = this.GetXYZByDistance(this.pipePoint[1], this.pipePoint[0], this.setDistance / 304.8);
			this.isShowMessage = IsShowMessage;
		}

		public GroupValueManage(Autodesk.Revit.DB.Document document, Pipe RefPipe, XYZ pointStart, XYZ endPoint, double distance, List<FamilySymbol> list)
		{
			this.nStartType = 1;
			this.doc = document;
			this.listFamilySymbol = list;
			this.setDistance = distance;
			this.pipe = RefPipe;
			this.diameter = this.pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
			this.pipePoint = YJKRevitTools.OrderPipePoint(this.pipe, pointStart);
			if (pointStart.Z < this.pipePoint[0].Z)
			{
				this.point = this.GetXYZByDistance(pointStart, this.pipePoint[0], this.setDistance / 304.8);
				return;
			}
			this.point = this.GetXYZByDistance(this.pipePoint[0], pointStart, this.setDistance / 304.8);
		}

		public bool Go()
		{
			List<FamilyInstance> fis = this.LayFamilySymbol();
			if (!this.CheckPipeLength(fis))
			{
				return false;
			}
			this.ConnPipeValve();
			return true;
		}

		public bool GoLayPressureGauge()
		{
			this.LayFamilySymbol();
			this.ConnPipeValve();
			return true;
		}

		public bool Go(Autodesk.Revit.DB.Document Doc, XYZ xyz, Line line, TextNoteType textNoteType, bool isLink)
		{
			if ((this.pipe.Location as LocationCurve).Curve.Length > 2.9527559055118111)
			{
				if (YJKRevitTools.isRiserPipe(this.pipe) && ((this.pipe.Location as LocationCurve).Curve.GetEndPoint(0).Z <= xyz.Z || (this.pipe.Location as LocationCurve).Curve.GetEndPoint(1).Z <= xyz.Z))
				{
					this.CreatDiameterAndType(Doc, this.pipe, xyz, line, textNoteType);
				}
				return true;
			}
			return false;
		}

		public Pipe GetPipe(Autodesk.Revit.DB.Document Doc, XYZ xyz, Line line, TextNoteType textNoteType)
		{
			Pipe result = null;
			List<FamilyInstance> list = this.LayFamilySymbol();
			if (!this.CheckPipeLength(list))
			{
				return result;
			}
			this.ConnPipeValve(Doc, xyz, line, textNoteType);
			FamilyInstance familyInstance = list.ElementAt(0);
			List<Connector> list2 = new List<Connector>();
			ConnectorSetIterator connectorSetIterator = familyInstance.MEPModel.ConnectorManager.Connectors.ForwardIterator();
			while (connectorSetIterator.MoveNext())
			{
				object obj = connectorSetIterator.Current;
				Connector item = obj as Connector;
				list2.Add(item);
			}
			Connector connector = (from p in list2.AsEnumerable<Connector>()
			orderby p.Origin.Z descending
			select p).FirstOrDefault<Connector>();
			if (connector.IsConnected)
			{
				ConnectorSetIterator connectorSetIterator2 = connector.AllRefs.ForwardIterator();
				while (connectorSetIterator2.MoveNext())
				{
					object obj2 = connectorSetIterator2.Current;
					Element owner = (obj2 as Connector).Owner;
					if (owner is Pipe)
					{
						result = (owner as Pipe);
					}
				}
			}
			return result;
		}

		private void Initial()
		{
			this.pipe = (this.doc.GetElement(this.refer) as Pipe);
			this.diameter = this.pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
			this.pipePoint = YJKRevitTools.OrderPipePoint(this.pipe, this.refer.GlobalPoint);
			this.point = this.GetXYZByDistance(this.pipePoint[1], this.pipePoint[0], this.setDistance / 304.8);
		}

		private void ChickPoint()
		{
			if (this.pipePoint[0].X < this.pipePoint[1].X)
			{
				XYZ item = this.pipePoint[0];
				XYZ item2 = this.pipePoint[1];
				this.pipePoint.Clear();
				this.pipePoint = new List<XYZ>();
				this.pipePoint.Insert(0, item2);
				this.pipePoint.Insert(1, item);
			}
		}

		private FamilySymbol GetFamilySymbol(FamilySymbol fs, double diameter)
		{
			FamilySymbol result = null;
			foreach (FamilySymbol familySymbol in fs.Family.GetSymbols())
			{
				double num = familySymbol.GetParameter("入口直径").AsDouble() * 304.8;
				result = familySymbol;
				if (Math.Abs(diameter - num) < 1E-08)
				{
					result = familySymbol;
					break;
				}
			}
			return result;
		}

		private double GetValueBodyLength(FamilySymbol fs1, FamilySymbol fs2)
		{
			double num = 0.0;
			Parameter parameter = fs1.GetParameter("主体长度");
			if (parameter == null)
			{
				parameter = fs1.GetParameter("管道长度");
			}
			if (parameter != null)
			{
				num += parameter.AsDouble();
			}
			Parameter parameter2 = fs2.GetParameter("主体长度");
			if (parameter2 == null)
			{
				parameter2 = fs2.GetParameter("管道长度");
			}
			if (parameter2 != null)
			{
				num += parameter2.AsDouble();
			}
			return num;
		}

		private XYZ GetXYZByDistance(XYZ startXYZ, XYZ endXYZ, double distance)
		{
			XYZ xyz = (endXYZ - startXYZ).Normalize();
			return startXYZ + distance * xyz;
		}

		public bool CheckPipeLength(List<FamilyInstance> fis)
		{
			double allValveLen = this.GetAllValveLen(this.listInstance);
			if (this.nStartType == 3)
			{
				double num = (this.pipePoint[0].DistanceTo(this.pipePoint[1]) - allValveLen) / 2.0;
				this.point = this.pipePoint[1] + num * (this.pipePoint[0] - this.pipePoint[1]).Normalize();
			}
			if (this.point.DistanceTo(this.pipePoint[0]) - allValveLen < UnitConvert.CovertToAPI(this.dMinDis, (Autodesk.Revit.DB.DisplayUnitType)2) || this.point.DistanceTo(this.pipePoint[1]) > this.pipePoint[1].DistanceTo(this.pipePoint[0]))
			{
				if (this.isShowMessage)
				{
					YJKMessageBox.Show("选择管道长度短，不满足组合阀件的放置连接！");
				}
				foreach (FamilyInstance familyInstance in fis)
				{
					this.doc.Delete(familyInstance.Id);
				}
				return false;
			}
			if (this.point.DistanceTo(this.pipePoint[1]) < UnitConvert.CovertToAPI(this.dMinDis, (Autodesk.Revit.DB.DisplayUnitType)2))
			{
				if (this.isShowMessage)
				{
					YJKMessageBox.Show("起始点距离管道端点太近，不满足组合阀门的放置连接！");
				}
				foreach (FamilyInstance familyInstance2 in fis)
				{
					this.doc.Delete(familyInstance2.Id);
				}
				return false;
			}
			return true;
		}

		public List<FamilyInstance> LayFamilySymbol()
		{
			List<FamilyInstance> list = new List<FamilyInstance>();
			foreach (FamilySymbol familySymbol in this.listFamilySymbol)
			{
				FamilyInstance familyInstance = YJKRevitTools.LayFamilySymbol(this.doc, new XYZ(0.0, 0.0, 0.0), familySymbol, 0.0);
				if (familyInstance != null)
				{
					this.listInstance.Add(familyInstance);
					list.Add(familyInstance);
				}
			}
			this.doc.Regenerate();
			foreach (FamilyInstance elem in this.listInstance)
			{
				if (elem.GetParameter(BuiltInParameter.RBS_FAMILY_CONTENT_ANNOTATION_DISPLAY) != null && !elem.GetParameter(BuiltInParameter.RBS_FAMILY_CONTENT_ANNOTATION_DISPLAY).IsReadOnly)
				{
					elem.GetParameter(BuiltInParameter.RBS_FAMILY_CONTENT_ANNOTATION_DISPLAY).Set(1);
				}
				if (elem.GetParameter("水管直径") != null && !elem.GetParameter("水管直径").IsReadOnly)
				{
					elem.GetParameter("水管直径").Set(this.diameter);
				}
				if (elem.GetParameter("公称直径") != null && !elem.GetParameter("公称直径").IsReadOnly)
				{
					elem.GetParameter("公称直径").Set(this.diameter);
				}
				if (elem.GetParameter("入口直径") != null && !elem.GetParameter("入口直径").IsReadOnly)
				{
					elem.GetParameter("入口直径").Set(this.diameter);
				}
				if (elem.GetParameter("出口直径") != null && !elem.GetParameter("出口直径").IsReadOnly)
				{
					elem.GetParameter("出口直径").Set(this.diameter);
				}
			}
			this.doc.Regenerate();
			return list;
		}

		public double GetDisVorderAndCon(FamilyInstance fin1)
		{
			double num = (double)(300 * this.doc.ActiveView.GetParameter(BuiltInParameter.VIEW_SCALE).AsInteger() / 100);
			num = UnitConvert.CovertToAPI(num, (Autodesk.Revit.DB.DisplayUnitType)2);
			if (fin1.GetParameter("阀体厚度") == null && fin1.GetParameter("长度") == null)
			{
				return 0.0;
			}
			double result;
			if (fin1.GetParameter("阀体厚度") != null)
			{
				result = (num - fin1.GetParameter("阀体厚度").AsDouble()) / 2.0;
			}
			else
			{
				result = (num - fin1.GetParameter("长度").AsDouble()) / 2.0;
			}
			return result;
		}

		public double GetDisVorderAndCon(FamilyInstance fin1, FamilyInstance fin2)
		{
			double num = (double)(300 * this.doc.ActiveView.GetParameter(BuiltInParameter.VIEW_SCALE).AsInteger() / 100);
			num = UnitConvert.CovertToAPI(num, (Autodesk.Revit.DB.DisplayUnitType)2);
			if (fin1.GetParameter("阀体厚度") == null && fin1.GetParameter("长度") == null)
			{
				return 0.0;
			}
			if (fin2.GetParameter("阀体厚度") == null && fin2.GetParameter("长度") == null)
			{
				return 0.0;
			}
			double num2;
			double num3;
			if (fin1.GetParameter("阀体厚度") != null)
			{
				num2 = fin1.GetParameter("阀体厚度").AsDouble() / 2.0;
				if (num - fin1.GetParameter("阀体厚度").AsDouble() < 0.0)
				{
					num3 = 0.0;
				}
				else
				{
					num3 = (num - fin1.GetParameter("阀体厚度").AsDouble()) / 2.0;
				}
			}
			else
			{
				num2 = fin1.GetParameter("长度").AsDouble() / 2.0;
				if (num - fin1.GetParameter("长度").AsDouble() < 0.0)
				{
					num3 = 0.0;
				}
				else
				{
					num3 = (num - fin1.GetParameter("长度").AsDouble()) / 2.0;
				}
			}
			if (fin2 != null)
			{
				if (fin2.GetParameter("阀体厚度") != null)
				{
					num2 += fin2.GetParameter("阀体厚度").AsDouble() / 2.0;
					if (num - fin2.GetParameter("阀体厚度").AsDouble() < 0.0)
					{
						num3 += 0.0;
					}
					else
					{
						num3 += (num - fin2.GetParameter("阀体厚度").AsDouble()) / 2.0;
					}
				}
				else
				{
					num2 += fin2.GetParameter("长度").AsDouble() / 2.0;
					if (num - fin2.GetParameter("长度").AsDouble() < 0.0)
					{
						num3 += 0.0;
					}
					else
					{
						num3 += (num - fin2.GetParameter("长度").AsDouble()) / 2.0;
					}
				}
			}
			if (num3 < UnitConvert.CovertToAPI(this.dMinDis, (Autodesk.Revit.DB.DisplayUnitType)2))
			{
				num3 = UnitConvert.CovertToAPI(this.dMinDis, (Autodesk.Revit.DB.DisplayUnitType)2);
			}
			num2 += num3;
			if (num2 - num < UnitConvert.CovertToAPI(this.dMinDis, (Autodesk.Revit.DB.DisplayUnitType)2))
			{
				num3 += UnitConvert.CovertToAPI(this.dMinDis, (Autodesk.Revit.DB.DisplayUnitType)2);
			}
			return num3;
		}

		public double GetAllValveLen(List<FamilyInstance> listfin)
		{
			double num = 0.0;
			double num2 = (double)(300 * this.doc.ActiveView.GetParameter(BuiltInParameter.VIEW_SCALE).AsInteger() / 100);
			num2 = UnitConvert.CovertToAPI(num2, (Autodesk.Revit.DB.DisplayUnitType)2);
			bool flag = false;
			for (int i = 0; i < listfin.Count; i++)
			{
				FamilyInstance elem = listfin[i];
				double num3 = 0.0;
				if (elem.GetParameter("阀体厚度") != null)
				{
					num3 = elem.GetParameter("阀体厚度").AsDouble();
				}
				else if (elem.GetParameter("长度") != null)
				{
					num3 = elem.GetParameter("长度").AsDouble();
				}
				if (num3 < num2)
				{
					num += num2;
					flag = false;
				}
				else
				{
					num += num3;
					num += (flag ? UnitConvert.CovertToAPI(this.dMinDis, (Autodesk.Revit.DB.DisplayUnitType)2) : 0.0);
					flag = true;
				}
			}
			return num;
		}

		public List<Pipe> ConnFirst(FamilyInstance fin1)
		{
			XYZ xyz = (this.pipePoint[0] - this.pipePoint[1]).Normalize();
			this.GetDisVorderAndCon(fin1);
			int num = this.nStartType;
			List<Connector> elementConnector = YJKRevitTools.GetElementConnector(fin1, (Autodesk.Revit.DB.Domain)3);
			if (elementConnector.Count < 2)
			{
				throw new Exception("阀件只有一个连接器！");
			}
			Connector connector = (elementConnector[0].Origin.X - elementConnector[1].Origin.X > 0.0) ? elementConnector[1] : elementConnector[0];
			ElementTransformUtils.MoveElement(this.doc, fin1.Id, this.point.Subtract(connector.Origin));
			Connector connector2 = this.GetConnectorByPoint(this.pipe, this.pipePoint[1]);
			XYZ xyz2 = connector2.CoordinateSystem.BasisZ.CrossProduct(connector.CoordinateSystem.BasisZ).Normalize();
			if (xyz2.IsAlmostEqualTo(new XYZ(0.0, 0.0, 0.0)))
			{
				xyz2 = new XYZ(0.0, 0.0, 1.0);
			}
			double degree = 360.0 - YJKRevitTools.GetTwoVectorAngle(connector2.CoordinateSystem.BasisZ, 1.0 * connector.CoordinateSystem.BasisZ, xyz2) * 180.0 / Math.PI;
			new YJKRotateFamilyInstance().RotateElem(fin1, this.point, xyz2, degree);
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(this.pipe);
			this.pipeList = hypiping.Separate(this.point);
			Connector connector3 = this.ConnFamiliy(fin1, this.point);
			XYZ basisZ = connector3.CoordinateSystem.BasisZ;
			Connector connector4 = null;
			foreach (Pipe pipe in this.pipeList)
			{
				connector4 = this.GetPipeConn(pipe, this.point, basisZ);
				this.connPipe = pipe;
				if (connector4 != null)
				{
					break;
				}
			}
			if (connector4 != null)
			{
				connector3.ConnectTo(connector4);
			}
			if (fin1 == this.listInstance[this.listInstance.Count - 1])
			{
				connector4 = this.GetElementUnConnector(fin1);
				connector2 = this.GetPipeConn(this.pipeList, this.connPipe, connector4.CoordinateSystem.BasisZ);
				connector4.ConnectTo(connector2);
				new XYZ(0.0, 0.0, 1.0);
				ElementTransformUtils.MoveElement(this.doc, fin1.Id, xyz);
				ElementTransformUtils.MoveElement(this.doc, fin1.Id, -1.0 * xyz);
			}
			return this.pipeList;
		}

		public void ConnSecond(FamilyInstance fin1, FamilyInstance fin2)
		{
			XYZ xyz = (this.pipePoint[0] - this.pipePoint[1]).Normalize();
			double disVorderAndCon = this.GetDisVorderAndCon(fin1, fin2);
			Connector elementUnConnector = this.GetElementUnConnector(fin1);
			List<Connector> elementConnector = YJKRevitTools.GetElementConnector(fin2, (Autodesk.Revit.DB.Domain)3);
			if (elementConnector.Count < 2)
			{
				throw new Exception("阀件只有一个连接器！");
			}
			Connector connector = (elementConnector[0].Origin.X < elementConnector[1].Origin.X) ? elementConnector[0] : elementConnector[1];
			ElementTransformUtils.MoveElement(this.doc, fin2.Id, elementUnConnector.Origin.Subtract(connector.Origin));
			XYZ xyz2 = (-1.0 * elementUnConnector.CoordinateSystem.BasisZ).CrossProduct(connector.CoordinateSystem.BasisZ).Normalize();
			if (xyz2.IsAlmostEqualTo(new XYZ(0.0, 0.0, 0.0)))
			{
				xyz2 = new XYZ(0.0, 0.0, 1.0);
			}
			double degree = 360.0 - YJKRevitTools.GetTwoVectorAngle(-1.0 * elementUnConnector.CoordinateSystem.BasisZ, connector.CoordinateSystem.BasisZ, xyz2) * 180.0 / Math.PI;
			new YJKRotateFamilyInstance().RotateElem(fin2, elementUnConnector.Origin, xyz2, degree);
			ElementTransformUtils.MoveElement(this.doc, fin2.Id, xyz * disVorderAndCon);
			connector = this.GetConnByBaseZ(fin2, elementUnConnector.CoordinateSystem.BasisZ);
			if (connector.Origin.Normalize().IsAlmostEqualTo(elementUnConnector.Origin.Normalize()))
			{
				elementUnConnector.ConnectTo(connector);
			}
			else
			{
				Pipe pipe = YJKRevitTools.NewPipe(this.pipeList[0], elementUnConnector.Origin, connector.Origin);
				Connector connectorByPoint = this.GetConnectorByPoint(pipe, elementUnConnector.Origin);
				elementUnConnector.ConnectTo(connectorByPoint);
				connectorByPoint = this.GetConnectorByPoint(pipe, connector.Origin);
				connector.ConnectTo(connectorByPoint);
			}
			if (fin2 == this.listInstance[this.listInstance.Count - 1])
			{
				elementUnConnector = this.GetElementUnConnector(fin2);
				Connector pipeConn = this.GetPipeConn(this.pipeList, this.connPipe, elementUnConnector.CoordinateSystem.BasisZ);
				elementUnConnector.ConnectTo(pipeConn);
			}
			new XYZ(0.0, 0.0, 1.0);
			ElementTransformUtils.MoveElement(this.doc, fin2.Id, xyz);
			ElementTransformUtils.MoveElement(this.doc, fin2.Id, -1.0 * xyz);
		}

		public void ConnPipeValve()
		{
			for (int i = 0; i < this.listInstance.Count; i++)
			{
				if (i == 0)
				{
					this.ConnFirst(this.listInstance[i]);
				}
				else
				{
					this.ConnSecond(this.listInstance[i - 1], this.listInstance[i]);
				}
			}
		}

		public void ConnPipeValve(Autodesk.Revit.DB.Document Doc, XYZ xyz, Line line, TextNoteType textNoteType)
		{
			int i = 0;
			while (i < this.listInstance.Count)
			{
				if (i == 0)
				{
					using (List<Pipe>.Enumerator enumerator = this.ConnFirst(this.listInstance[i]).GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							Pipe pipe = enumerator.Current;
							if (YJKRevitTools.isRiserPipe(pipe) && ((pipe.Location as LocationCurve).Curve.GetEndPoint(0).Z <= xyz.Z || (pipe.Location as LocationCurve).Curve.GetEndPoint(1).Z <= xyz.Z))
							{
								this.CreatDiameterAndType(Doc, pipe, xyz, line, textNoteType);
							}
						}
						goto IL_C2;
					}
					goto IL_A2;
				}
				goto IL_A2;
				IL_C2:
				i++;
				continue;
				IL_A2:
				this.ConnSecond(this.listInstance[i - 1], this.listInstance[i]);
				goto IL_C2;
			}
		}

		public void CreatDiameterAndType(Autodesk.Revit.DB.Document Doc, Pipe pipe, XYZ point, Line line, TextNoteType textNoteType)
		{
			FamilySymbol familySymbol = this.GetFamilySymbol(Doc, "MarkTag", "HY_水管标注.rfa", "水管管径");
			FamilySymbol familySymbol2 = this.GetFamilySymbol(Doc, "MarkTag", "HY_水管标注.rfa", "水管类型");
			double length = (pipe.Location as LocationCurve).Curve.Length;
			double z = point.Z;
			double num = pipe.GetParameter(BuiltInParameter.RBS_PIPE_DIAMETER_PARAM).AsDouble();
			XYZ xyzpoint = line.Project(point).XYZPoint;
			XYZ pnt = new XYZ(xyzpoint.X, xyzpoint.Y, z) - Doc.ActiveView.UpDirection * (length * 2.0 / 3.0) - Doc.ActiveView.RightDirection * (num + 0.49212598425196846 * (double)this.doc.ActiveView.Scale / 100.0);
			XYZ pnt2 = new XYZ(xyzpoint.X, xyzpoint.Y, z) - Doc.ActiveView.UpDirection * (length / 3.0) - Doc.ActiveView.RightDirection * (-0.0065616797900262466 * (double)this.doc.ActiveView.Scale / 100.0);
			IndependentTag independentTag = RevitVersionFuncs.NewTag(Doc, Doc.ActiveView, new Reference(pipe), false, 0, 0, pnt);
			Element element = RevitVersionFuncs.NewTag(Doc, Doc.ActiveView, new Reference(pipe), false, 0, 0, pnt2);
			independentTag.ChangeTypeId(familySymbol.Id);
			element.ChangeTypeId(familySymbol2.Id);
		}

		private FamilySymbol GetFamilySymbol(Autodesk.Revit.DB.Document Doc, string typeName, string familyName, string familySymbolName)
		{
			FamilySymbol result = null;
			string text = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(Doc), typeName, familyName);
			Family family = null;
			SubTransaction subTransaction = null;
			try
			{
				foreach (Element element in YJKElementFilter.GetElementByCategory(Doc, typeof(FamilySymbol), BuiltInCategory.OST_PipeTags))
				{
					if ((element as FamilySymbol).Family.Name == familyName.Substring(0, familyName.IndexOf(".")) && element.Name == familySymbolName)
					{
						FamilySymbol result2;
						result = (result2 = (element as FamilySymbol));
						return result2;
					}
				}
				subTransaction = new SubTransaction(Doc);
				subTransaction.Start();
				Doc.LoadFamily(text, out family);
				subTransaction.Commit();
			}
			catch (Exception)
			{
			}
			finally
			{
				if (subTransaction != null && (int)subTransaction.GetStatus() != 3)
				{
					subTransaction.RollBack();
				}
			}
			if (family != null)
			{
				foreach (FamilySymbol familySymbol in family.GetSymbols())
				{
					if (familySymbol.Name == familySymbolName)
					{
						result = familySymbol;
						break;
					}
				}
			}
			return result;
		}

		private Connector GetPipeConn(List<Pipe> pipeList, Pipe otherPipe, XYZ vector)
		{
			Pipe piping = null;
			foreach (Pipe pipe in pipeList)
			{
				if (pipe != otherPipe)
				{
					piping = pipe;
					break;
				}
			}
			YJKPiping<Pipe> hypiping = new YJKPiping<Pipe>(piping);
			if (hypiping.StartConnector.Connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-1.0 * vector))
			{
				return hypiping.StartConnector.Connector;
			}
			return hypiping.EndConnector.Connector;
		}

		public Pipe GetEndPipe()
		{
			Pipe result = null;
			foreach (Pipe pipe in this.pipeList)
			{
				if (pipe != this.connPipe)
				{
					result = pipe;
					break;
				}
			}
			return result;
		}

		private Connector GetConnByBaseZ(FamilyInstance fi, XYZ baseZ)
		{
			foreach (Connector connector in YJKRevitTools.GetElementConnector(fi, (Autodesk.Revit.DB.Domain)3))
			{
				if (connector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-1.0 * baseZ))
				{
					return connector;
				}
			}
			return null;
		}

		private Connector GetConnectorByPoint(Pipe pipe, XYZ point)
		{
			return new YJKPiping<Pipe>(pipe).GetCloseConnector(point, false);
		}

		private Connector ConnFamiliy(FamilyInstance fi, XYZ layPoint)
		{
			Connector result = null;
			foreach (Connector connector in YJKRevitTools.GetElementConnector(fi, (Autodesk.Revit.DB.Domain)3))
			{
				if (connector.Origin.IsAlmostEqualTo(layPoint))
				{
					result = connector;
				}
			}
			return result;
		}

		private Connector GetPipeConn(Pipe pipe, XYZ layPoint, XYZ vector)
		{
			Connector closeConnector = new YJKPiping<Pipe>(pipe).GetCloseConnector(layPoint, false);
			if (closeConnector.CoordinateSystem.BasisZ.IsAlmostEqualTo(-1.0 * vector))
			{
				return closeConnector;
			}
			return null;
		}

		private Connector GetElementUnConnector(Element elem)
		{
			Connector result = null;
			if (elem is FamilyInstance)
			{
				ConnectorSetIterator connectorSetIterator = (elem as FamilyInstance).MEPModel.ConnectorManager.Connectors.ForwardIterator();
				while (connectorSetIterator.MoveNext())
				{
					object obj = connectorSetIterator.Current;
					Connector connector = obj as Connector;
					if (connector != null && !connector.IsConnected && connector.Domain == (Autodesk.Revit.DB.Domain)3)
					{
						result = connector;
						break;
					}
				}
			}
			return result;
		}

		private Document doc;

		private Reference refer;

		private List<FamilySymbol> listFamilySymbol;

		private double setDistance;

		private double diameter;

		private int nStartType = -1;

		public double dMinDis = 50.0;

		private Pipe pipe;

		private XYZ point;

		private IList<XYZ> pipePoint;

		private List<FamilyInstance> listInstance = new List<FamilyInstance>();

		private List<Pipe> pipeList;

		private Pipe connPipe;

		public bool isShowMessage = true;
	}
}
